package com.venus.common.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectUtil 
{
	public ReflectUtil() 
	{
	}
    
    public static String getMethodname(String fname,int type)//1set,2get
    {
        if(type==1)
        {
        	return "set"+fname.substring(0,1).toUpperCase()+fname.substring(1);
        }else{
        	return "get"+fname.substring(0,1).toUpperCase()+fname.substring(1);	
        }
    }
   
    public static Method getSetMethod(Class c,String fname,int type)
    { 
      Method method=null;
      String methodname=getMethodname(fname,type);
      Method[] ms=c.getDeclaredMethods();
  	  for(int i=0;i<ms.length;i++)
  	  {
  	    Method m=ms[i];
  	    String mname=m.getName();
  	    if(methodname.equals(mname))
  	    {
  	    	method=m;
  	    }
  	  }
  	  return method;
    }

    public static Object InvokeGetmethod(Object o,String fname){
    	Class c=o.getClass();
    	Method m=getSetMethod(c,fname,2);
    	Object obj=null;
    	try {
			obj=m.invoke(o, new Object[0]);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return obj;
    }

    public static void InvokeSetmethod(Object obj,String fname,String type,String value)
    {
    	    Object[] paras=new Object[1];
    	    
    	    Class c=obj.getClass();
        	Method m=getSetMethod(c,fname,1);
        	paras[0]=value;
            if(type.indexOf("int")!=-1)
            {
            	paras[0]=new Integer(value);	
            }else if(type.indexOf("byte")!=-1){
            	paras[0]=new Byte(value);	
            }else if(type.indexOf("short")!=-1){
            	paras[0]=new Short(value);	
            }else if(type.indexOf("long")!=-1){
            	paras[0]=new Long(value);	
            }else if(type.indexOf("boolean")!=-1){
            	paras[0]=new Boolean(value);	
            }else if(type.indexOf("double")!=-1){
            	paras[0]=new Double(value);	
            }else if(type.indexOf("float")!=-1){
            	paras[0]=new Float(value);	
            }
        	try{
				m.invoke(obj,paras);
			}catch(IllegalArgumentException e) {
				e.printStackTrace();
			}catch(IllegalAccessException e) {
				e.printStackTrace();
			}catch(InvocationTargetException e) {
				e.printStackTrace();
			}
    }
    
    public static void main(String args[])
    {
//       String sql="select * from usergroup";
//       try {
//		List list=getObjs(UsergroupActionForm.class,sql);
//		for(int i=0;i<list.size();i++)
//		{
//		    UsergroupActionForm form=(UsergroupActionForm)list.get(i);	
//		    System.out.println(form.getGpid()+"--"+form.getGpname()+"---"+form.getGpnum()+"---"+form.getGpdesc());
//		}
//	   }catch (InstantiationException e) {
//		e.printStackTrace();
//	   }catch (IllegalAccessException e) {
//		e.printStackTrace();
//	   }
//    	 String sql="select * from userinfo";
//        
//  		List list=getObjs(UserinfoActionForm.class,sql);
//  		for(int i=0;i<list.size();i++)
//  		{
//  			UserinfoActionForm form=(UserinfoActionForm)list.get(i);	
//  		    System.out.println(form.getUid()+"--"+form.getUname()+"---"+form.getPasswd()+"---"+form.getEmail()+"--"+form.getStatus()+"---"+form.getEmpid()+"---"+form.getGpid());
//  		}
	   
    }
}
